﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLVT.Entity;
using QLVT.Config;

namespace QLVT.DAO
{
    class MatHangDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from MatHang");
        }

        public MatHang GetById(String mahang)
        {
            MatHang mh = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from MatHang where mahang = '" + mahang + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    mh = new MatHang();
                    mh.MaHang = dataReader.GetString(0);
                    mh.TenHang = dataReader.GetString(1);
                    mh.SoLuongHienCo = dataReader.GetInt32(2);
                    mh.GiaBanHienThoi = dataReader.GetInt32(3);
                    mh.Dvt = dataReader.GetString(4);
                    mh.MaKho = dataReader.GetString(5);
                    mh.Mancc = dataReader.GetString(6);
                    return mh;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
               
            }
            return mh;
        }

        public bool DeleteById(String mahang)
        {
            return Update("delete from MatHang where mahang = '" + mahang + "'");
        }

        public bool UpdateById(MatHang mh)
        {
            String sql = "update MatHang "
                 + "set mahang = '" + mh.MaHang + "'"
                 + ", tenhang = '" + mh.TenHang + "'"
                 + ", slhc = '" + mh.SoLuongHienCo + "'"
                 + ", gbht = '" + mh.GiaBanHienThoi + "'"
                 + ", dvt = '" + mh.Dvt + "'"
                 + ", makho = '" + mh.MaKho + "'"
                 + ", mancc = '" + mh.Mancc + "'"
                 + " where mahang = '" + mh.MaHang + "'";
            return Update(sql);
        }
    }
}
